bash基础 您所在的位置:网站首页 grep 正则表达式匹配 bash基础

bash基础

2024-03-16 05:07| 来源: 网络整理| 查看: 265

grep

grep全称:Globally search a Regular Expression and Print 全局搜索正则表达式

正规表达式本质上是一种"表示方法", 只要工具程序支持这种表示方法,那么该工具就可以使用正则表达式处理字符串。 例如 vi, grep, awk ,sed 等等工具,因为她们有支持正规表示法, 所以,这些工具就可以使用正规表示法的特殊字节来进行字串的处理。但例如 cp, ls 等命令并未支持正规表示法, 所以就只能使用 bash 自己本身的wildcards(通配符)。通配符与正则表达式是两个完全不同的东西,通配符只是bash提供的一个功能,正则表达式是一种字符串处理的表达方式。千万不要将而这混淆。

正则表达式在表示字符串时,依据不同的严谨程度可以分为基础正则表达式和扩展正则表达式。

grep在查找字符串的时候,是以整行为单位进行数据选取的。举例:一个文件有10行,其中2行包含匹配关键字,那么grep只输出命中的那两行,其他全部丢掉。

--color=auto   将匹配到的关键字高亮。一般来说这个不用我们指定,因为Linux distribution已经为我们alias指定好了。

-n:显示命中关键字在原文件中的行数

-v显示非关键字的那些行

[root@51cto ~]# cat uniqtest -n 1 wang student 23 2 wang student 23 3 han teacher 38 4 liu teacher 29 5 zhang student 23 6 wang student 23 [root@51cto ~]# grep "wang" uniqtest -v han teacher 38 liu teacher 29 zhang student 23 View Code

-o只显示关键字

[root@51cto ~]# cat uniqtest -n 1 wang student 23 2 wang student 23 3 han teacher 38 4 liu teacher 29 5 zhang student 23 6 wang student 23 [root@51cto ~]# grep "wang" uniqtest -o wang wang wang View Code

-i 不区分大小写

[root@51cto ~]# cat uniqtest wang student 23 wang student 23 han teacher 38 liu teacher 29 zhang student 23 wang student 23 Wang student 24 [root@51cto ~]# grep "wang" uniqtest -i wang student 23 wang student 23 wang student 23 Wang student 24 View Code

-A# 将命中的那一行下面#行也显示出来;

-B#  将命中的那一行上面#行也显示出来。如果,则

-C#  前后都要显示#行

1只是做个例子,可以是任何数字

[root@51cto ~]# cat uniqtest wang student 23 wang student 23 han teacher 38 liu teacher 29 zhang student 23 wang student 23 Wang student 24 [root@51cto ~]# grep "wang" uniqtest -A1 wang student 23 wang student 23 han teacher 38 -- wang student 23 Wang student 24 View Code

-q 安静模式,不现实任何输出

[root@localhost ~]# cat >a.txt nihaooo > hello > EOF [root@localhost ~]# if grep -q hello a.txt ; then echo yes;else echo no; fi yes [root@localhost ~]# if grep -q word a.txt; then echo yes; else echo no; fi no View Code

-E 表示后面的表达式是扩展的正则表达式

基本正则表达式

参考:bash功能——命令行编辑、内部命令 外部命令、命令补全 、命令历史、文件名通配符、命令别名 文件名通配符

文件名通配符

* 代表任意长度任意字符

?代表任意单个字符

[] 指定范围的任意单个字符

[^ ] 指定范围外的任意单个字符

正则表达式元字符

. 匹配任意单个字符

[] 匹配指定范围内的任意单个字符

[^ ] 匹配指定范围外的任意单个字符

正则表达式匹配次数

+ 匹配前面≥1次

* 匹配前一个字符≥0次

? 匹配前一个字符0次或1次

\{m,n\} 匹配前一个字符最少m次 最多n次

  \{0,n\} 最多n次

  \{m\}  最少m次

更新——Thu Jun 14 22:20:33 EDT 2018

上面这个结果一直不理解,今天突然对这个问题有了新的理解

以前我的理解(错误的)

a[a-z]\{0,2\}c        a后面跟26个字母中任何一个,后面\{0,2\}限定这个字母出现次数(0-2次)。那么这样的话asssc也应该命中,但实际上没有,这个困扰我好久

现在理解(正确)

a[a-z]\{0,2\}c       [a-z]\{0,2\}应该看成一组一起理解,这一组可以表示aa,ab这种,加下图

更新——Thu Jun 14 22:20:33 EDT 2018

任意长度任意字符 .*

 分组匹配

[root@51cto ~]# cat student zhangsan,math=good,english=band hanligang,math=good,english=good yixiu,math=bad,english=good xiaomo,math=bad,english=bad,music=good,geography=good yuanzi,math=good,english=bad,music=good,geography=good View Code

匹配数学、英语成绩一样的人(即数学好英语就好,数学坏英语就坏)使用了向后引用

grep '.*,math=\(.*\),english=\1' student --color View Code

匹配数学成绩和英语成绩一样,音乐成绩和地理成绩一样。使用了向后引用

grep '.*,math=\(.*\),english=\1,music=\(.*\),geography=\2' student --color View Code

锚定符

[root@51cto ~]# cat re.txt root chroot rooter this is root there is chroot View Code

\  锚定词尾

\ 锚定单词

^ 锚定行首

$ 锚定行尾

 锚定行首 前面有空格的root开头的行

锚定行尾 有标点符号

字符类

POSIX定义了一些只能在正则表达式中使用的字符类

alnum   字母和数字

alpha 字母

blank 仅表示空格或制表符

cntrl   控制字符

digit   十进制数

graph 打印字符,不包含空格

lower 小写字母

print   打印字符,包含空格

punct 打印字符,不包含字母和数字

space   空白

upper   大写字母

xdigit                十六进制数

使用方法:

[[:space:]]

[[:digit:]]

[[:alpha:]]

echo '.,.,?.!123abcABC abcA123..?1 2' | grep '[[:punct:]]\{4,8\}[[:digit:]]\{1,3\}[[:lower:]]*[[:upper:]]*[[:space:]]*[[:graph:]]*' --color=auto View Code

 扩展正则表达式

扩展正则表达式元字符(与基本正则表达式一样)

.

[]

[^]

匹配次数

*

+ 至少匹配一次

{m,n} 括号前不需要转义字符\

锚定符

^ 行首锚定

$ 行尾锚定

\ 词尾锚定新增  \b

或者

表示符号  |

分组

分组不用转义

查找a=c b=d的行

echo 'a=10b=20c=10d=20' | egrep 'a=(..)b=(..)c=\1d=\2' --color View Code

这里使用的都是egrep,使用grep -E效果是一样的

 fgrep

不解析正则表达式 直接搜索文本

写正则表达式需要注意的两点

1.       转义字符 \

将正则表达式中元字符 转换成字符本身

2.       如果正则表达式中有命令替换 正则表达式只能使用” ”弱引用

如果正则表达式中没有命令替换 ‘ ’ “”

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有